﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBSourceToolsLib;

namespace DBSourceTools
{
    public partial class CompareDatabasesForm : Form
    {
        public string SourceDatabaseNick;
        public string TargetDatabaseNick;

        Hashtable m_ServerHash = new Hashtable();
        public CompareDatabasesForm()
        {
            InitializeComponent();
        }

        private void CompareDatabasesForm_Load(object sender, EventArgs e)
        {
            UpdateListViews();
        }

        private void UpdateListViews()
        {
            
            m_treeViewSource.Nodes.Clear();
            m_treeViewTarget.Nodes.Clear();
            SourceDatabase oCurrentSource = null; 
            TreeNode oSourceSelectedNode = null;
            if (!String.IsNullOrEmpty(SourceDatabaseNick))
            {
                oCurrentSource = UserSettings.Instance().getSourceDatabase(SourceDatabaseNick);
            }

            DBSourceToolsLib.DatabaseProject oProject = DBSourceToolsLib.UserSettings.Instance().DatabaseProject;
            // add source databases first
            foreach (DBSourceToolsLib.SourceDatabase oSource in oProject.SourceDatabases)
            {
                AddDatabase(oSource.DataSourceName, oSource.DatabaseName, oSource.NickName);
            }
            // add target databases
            foreach (DBSourceToolsLib.SourceDatabase oSource in oProject.SourceDatabases)
            {
                foreach (DBSourceToolsLib.TargetDatabase oTarget in oSource.TargetDatabases)
                {
                    AddDatabase(oTarget.DataSourceName, oTarget.DatabaseName, oTarget.NickName);
                }
            }

            DBSourceToolsLib.DatabaseEx oDbEx;
            foreach (string strKey in m_ServerHash.Keys)
            {
                TreeNode oServerNode = m_treeViewSource.Nodes.Add(strKey, strKey, 0, 0);

                TreeNode oServerTargetNode = m_treeViewTarget.Nodes.Add(strKey, strKey, 0, 0);
                List<DBListItem> dbList = (List<DBListItem>)m_ServerHash[strKey];

                foreach (DBListItem oDBListItem in dbList)
                {
                    oDbEx = DBSourceToolsLib.UserSettings.Instance().DatabaseProject.GetDatabaseEx(oDBListItem.NickName);

                    TreeNode dbNode = oServerNode.Nodes.Add(oDbEx.DatabaseName, oDbEx.DatabaseName, 1, 1);
                    dbNode.Tag = oDBListItem.NickName; // nickname
                    if (oCurrentSource != null)
                    {
                        if (oDbEx.DatabaseName.CompareTo(oCurrentSource.DatabaseName) == 0)
                            if (oDbEx.DataSourceName.CompareTo(oCurrentSource.DataSourceName) == 0)
                                oSourceSelectedNode = dbNode;
                    }


                    TreeNode dbTargetNode = oServerTargetNode.Nodes.Add(oDbEx.DatabaseName, oDbEx.DatabaseName, 1, 1);
                    dbTargetNode.Tag = oDBListItem.NickName; // nickname
                }

            }
            //m_treeViewSource.ExpandAll();
            if (oSourceSelectedNode != null)
                m_treeViewSource.SelectedNode = oSourceSelectedNode;
                //m_treeViewSource_AfterSelect(null, new TreeViewEventArgs(oSourceSelectedNode));
        }

        private void AddDatabase(string strServer, string strDatabase, string strNick)
        {
            string strKey = strServer;
            if (strKey.CompareTo(".") == 0)
            {
                strKey = "(local)";
            }
            if (m_ServerHash.ContainsKey(strKey))
            {
                List<DBListItem> dbList = (List<DBListItem>)m_ServerHash[strKey];
                bool bFound = false;
                foreach (DBListItem oItem in dbList)
                {
                    if ((oItem.Server.CompareTo(strServer) == 0)
                        && (oItem.Database.CompareTo(strDatabase) == 0))
                    {
                        bFound = true;
                        break;
                    }
                }
                if (!bFound)
                {
                    DBListItem oNewItem = new DBListItem(strServer, strDatabase, strNick);
                    dbList.Add(oNewItem);
                    m_ServerHash[strKey] = dbList;

                }
            }
            else
            {
                List<DBListItem> dbList = new List<DBListItem>();
                DBListItem oNewItem = new DBListItem(strServer, strDatabase, strNick);
                dbList.Add(oNewItem);
                m_ServerHash.Add(strKey, dbList);
            }
            
        }

        private void m_treeViewSource_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                SourceDatabaseNick = (string)e.Node.Tag;
                m_textBoxSource.Text = "\\\\" + e.Node.Parent.Name + "\\" + e.Node.Text;
            }
        }

        private void m_treeViewTarget_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                TargetDatabaseNick = (string)e.Node.Tag;
                m_textBoxTarget.Text = "\\\\" + e.Node.Parent.Name + "\\" + e.Node.Text;
            }

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonCompare_Click(object sender, EventArgs e)
        {
            // TODO : COMPLETE 1.2.0.8 cannot compare SQL 2000 databases. (PeteBSC)
            if (String.IsNullOrEmpty(m_textBoxTarget.Text))
            {
                MessageBox.Show("Please select a Target Database");
            } else
                if (String.IsNullOrEmpty(m_textBoxSource.Text))
                {
                    MessageBox.Show("Please select a Source Database");

                }
                else
                {
                    EnsureTargetIsASourceDatabase();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }

        }

        private void EnsureTargetIsASourceDatabase()
        {
            SourceDatabase osource = UserSettings.Instance().getSourceDatabase(TargetDatabaseNick);
            if (osource == null)
            {
                DatabaseEx oDBEx = UserSettings.Instance().DatabaseProject.GetDatabaseEx(TargetDatabaseNick);

                // check that this nick is unique.

                UserSettings.Instance().DatabaseProject.AddNewDatabase(oDBEx);
                UserSettings.Instance().CurrentDbNickName = oDBEx.NickName;
                UserSettings.Instance().SaveConfig();
                UserSettings.Instance().DatabaseProject.WriteProjectFile();

                UpdateListViews();

            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (UserSettings.Instance().DatabaseProject == null)
                {
                    MessageBox.Show("Please create a Database Project File>New>Project before adding a Source Database.");
                    return;
                }
                // Database | Add | New Source Database
                DBSourceToolsLib.DatabaseProjectUI.NewDatabaseForm oNewDbForm = new DBSourceToolsLib.DatabaseProjectUI.NewDatabaseForm(true, false);

                if (oNewDbForm.ShowDialog(this) == DialogResult.OK)
                {
                    DBConnectionLib.Database oNewSourceDb = oNewDbForm.GetDatabaseObject();

                    // check that this nick is unique.

                    UserSettings.Instance().DatabaseProject.AddNewDatabase(oNewSourceDb);
                    UserSettings.Instance().CurrentDbNickName = oNewSourceDb.NickName;
                    UserSettings.Instance().SaveConfig();
                    UserSettings.Instance().DatabaseProject.WriteProjectFile();

                    UpdateListViews();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
    }


}
